package com.wn.class30.service.impl;

import com.wn.class30.entity.Producttype;
import com.wn.class30.entity.ProducttypeExample;
import com.wn.class30.mapper.ProducttypeDao;
import com.wn.class30.service.BackProducttypeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;

@Service
public class BackProducttypeServiceImpl implements BackProducttypeService {
    @Resource
    private ProducttypeDao producttypeDao;

    @Override
    public int selectCount(HashMap map) {
        ProducttypeExample ex = getExample(map);
        return (int) producttypeDao.countByExample(ex);
    }

    @Override
    public Object select(HashMap map) {
        ProducttypeExample ex = getExample(map);
        return producttypeDao.selectByExample(ex);
    }

    @Override
    public void deleteById(Integer producttypeId) throws Exception {
        producttypeDao.deleteByPrimaryKey(producttypeId);
    }

    @Override
    public void updateProducttype(Producttype producttype) throws Exception {
        producttypeDao.updateByPrimaryKey(producttype);
    }

    @Override
    public void insertProducttype(Producttype producttype) throws Exception {
        producttypeDao.insert(producttype);
    }

    public ProducttypeExample getExample(HashMap map) {
        ProducttypeExample ex = new ProducttypeExample();

        String name = (String) map.get("name");

        Integer page = 0;
        if (!"".equals((String) map.get("page")) && (String) map.get("page") != null) {
            page = Integer.parseInt((String) map.get("page"));
        }
        Integer limit = 0;
        if (!"".equals((String) map.get("limit")) && (String) map.get("limit") != null) {
            limit = Integer.parseInt((String) map.get("limit"));
        }
        ex.setOffset(((long) (page - 1)) * limit);
        ex.setLimit(limit);
        ProducttypeExample.Criteria criteria = ex.createCriteria();
        if (!"".equals(name) && name != null) {
            criteria.andProducttypeNameLike("%" + name + "%");
        }
        return ex;
    }
}
